const App = getApp();
const onHandToofg = require('../../utils/util')
const httpRequest=require('../../utils/httpRequest.js')
Page({
  data: {
    avatar_url: '',
    nickname: '',
    evaluate: [],
    Token:''
  },

  onLoad() {
    const userToken = wx.getStorageSync('userToken'); 
    this.setData({
      avatar_url: App.globalData.avatar_url,
      nickname: App.globalData.nickname,
      Token: userToken
    });
    this.getcomment();
  },

 // 获取评论信息
  getcomment() {
    this.getUserInfo();
    httpRequest.Request('GET',App.globalData.backend_add+'api/comments',{},{'Authorization': this.data.Token},'evaluate','null',this)
  },

  // 获取用户头像
  onChooseAvatar(e) {
    console.log(e);
    this.setData({
      avatar_url: e.detail.avatarUrl
    })
    App.globalData.avatar_url=e.detail.avatarUrl;
    this.saveUserInfo();
    this.uploadUserInfo();
    
  },
  // 选择昵称
  onChooseNickname(e) {
    
    this.setData({
      nickname: e.detail.value
    })
    App.globalData.nickname=e.detail.value;
    this.saveUserInfo();
    if(this.data.nickname !== '匿名用户'){
      this.uploadUserInfo();
    } 
  },
  // 存储用户信息
  saveUserInfo() {
    wx.setStorage({
      key: 'userInfo',
      data: {
        avatarUrl: this.data.avatar_url,
        nickName: this.data.nickname
      }
    })
  },

  // 获取用户信息
  getUserInfo() {
    console.log(this, '第一行');
    let that = this;
    wx.getStorage({
      key: 'userInfo',
      success: function(res) {
        console.log(that, '函数内');
        that.setData({
          avatar_url: res.data.avatarUrl,
          nickname: res.data.nickName
        })
      }
    })
  },

//点击进入订单页
  onHandTodingdan(){
    onHandToofg.onHandToofg()
  },

  // 上传用户信息到后台
uploadUserInfo() {
  wx.request({
    url: App.globalData.backend_add + 'api/user/info',
    method: 'POST',
    header: {
      'Authorization': this.data.Token,
      'Content-Type': 'application/json'
    },
    data: {
      avatarUrl: this.data.avatar_url,
      nickName: this.data.nickname,
      token: this.data.Token
    },
    success: function (res) {
      console.log('上传成功：', res);
    },
    fail: function (err) {
      console.error('上传失败：', err);
    }
  });
}

})